//
//  workerManager.cpp
//  职工管理系统
//
//  Created by 吴尚远 on 2022/5/13.
//

#include "workerManager.hpp"

//初始化属性
WorkerManager::WorkerManager()
{
    //1.文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in);//读文件
    if (!ifs.is_open())//返回值是bool型
    {
        cout << "文件不存在" << endl;
        //初始化记录人数
        this->m_EmpNum = 0;
        //初始化数组指针
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
   //文件存在，但数据为空
    char ch;
    ifs >> ch;
    if(ifs.eof())
    {
        cout << "文件为空" << endl;
        //初始化记录人数
        this->m_EmpNum = 0;
        //初始化数组指针
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    
    //文件存在，并且记录数据
    int num = this->get_EmpNum();
    
    //cout << "职工人数为：" << num << endl;
    
    this->m_EmpNum = num;
    
    //开辟空间
    this->m_EmpArray = new Worker*[this->m_EmpNum];
    //将文件中的数据存到数组中
    this->init_Emp();
    //更新职工不为空标志
    this->m_FileIsEmpty = false;
    
//    for(int i = 0;i < this->m_EmpNum; i++)
//    {
//        cout << "职业编号：" << this->m_EmpArray[i]->m_id << " "
//        << "姓名：" << this->m_EmpArray[i]->m_name << " "
//        << "部门编号：" << this->m_EmpArray[i]->m_DeptId << endl;
//
//    }
}

WorkerManager::~WorkerManager()
{
    if(this->m_EmpArray != NULL)
    {
        for(int i = 0; i < this->m_EmpNum; i++)
        {
            if(this->m_EmpArray[i] != NULL)
            {
                delete this->m_EmpArray[i];
            }
        }
        delete [] this->m_EmpArray;
        this->m_EmpArray = NULL;
    }
}

//统计文件中人数
int WorkerManager::get_EmpNum()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);//打开文件 -- 读
    int id;
    string name;
    int dId;
    
    int num = 0;//记录人数
    while (ifs >> id && ifs >> name && ifs >> dId)
    {
        num++;
    }
    return num;
}
//初始化员工
void WorkerManager::init_Emp()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);

    int id;
    string name;
    int dId;
    int index = 0;
    while(ifs >> id && ifs >> name && ifs >> dId)
    {
        Worker * worker = NULL;
        if (dId == 1) //普通员工
        {
            worker = new Employee(id, name, dId);
        }
        if (dId == 2) //经理
        {
            worker = new Manager(id, name, dId);
        }
        if (dId == 3) //老板
        {
            worker = new Boss(id, name, dId);
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    ifs.close();
}

void WorkerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out);//用输出的方式打开文件 -- 写文件
    for(int i = 0; i < this->m_EmpNum; i++)
    {
        ofs << this->m_EmpArray[i]->m_id << " "
        << this->m_EmpArray[i]->m_name << " "
        <<this->m_EmpArray[i]->m_DeptId << endl;
    }
    //关闭文件
    ofs.close();
}

//添加职工
void WorkerManager::Add_Emp()
{
    //功能描述：批量添加职工，并且保存到文件中

    //用户在批量创建时，可能会创建不同种类的职工
    //如果想将所有不同种类的员工都放入到一个数组中，可以将所有员工的指针维护到一个数组里
    //如果想在程序中维护这个不定长度的数组，可以将数組创建到堆区，并利用Worker **的指针维护
    cout << "请输入添加职工数量：" << endl;
    int addNum = 0;//保存用户的输入数量
    cin >> addNum;
    if (addNum > 0)
    {
        //计算新空间的大小
        int newSize = this->m_EmpNum + addNum;
        //开辟新空间
        Worker ** newSpace = new Worker*[newSize];
        //将原空间下内容拷贝到新空间下
        if (this->m_EmpArray != NULL)
        {
            for(int i = 0; i < this->m_EmpNum;i++)
            {
                newSpace[i] = this->m_EmpArray[i];
            }
        }
        //输入新数据
        for(int i = 0; i < addNum; i++)
        {
            int id;//职工编号
            string name;//职工姓名
            int dSelect;//部门选择
            
            cout << "请输入第 " << i + 1 << "个新职工编号：" << endl;
            cin >> id;
            cout << "请输入第 " << i + 1 << "个新职工姓名：" << endl;
            cin >> name;
            cout << "请选择该职工岗位：" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            cin >> dSelect;
            Worker * worker = NULL;
            switch (dSelect) {
                case 1:
                    worker = new Employee(id, name, 1);
                    break;
                case 2:
                    worker = new Manager(id, name, 2);
                    break;
                case 3:
                    worker = new Boss(id, name, 3);
                    break;
                default:
                    break;
                    
            }
            //创建职工职责，保存到数组中
            newSpace[this->m_EmpNum+i] = worker;
        }
        //释放原有空间
        delete [] this->m_EmpArray;
        //更改新空间的指向
        this->m_EmpArray = newSpace;
        //更新新的职工人数
        this->m_EmpNum = newSize;
        //更新职工不为空标志
        this->m_FileIsEmpty = false;
        //提示添加成功
        cout << "成功添加" << addNum << "名新职工" << endl;
        //保存数据到文件
        this->save();
        
    }
    else
    {
        cout << "请重新输入" << endl;
    }
}

//显示函数
void WorkerManager::Show_Emp()
{
    //判断文件是否为空
    if(this->m_FileIsEmpty)
    {
        cout << "文件不存在或被记录为空！" << endl;
    }
    else
    {
        for(int i = 0; i < m_EmpNum; i++)
        {
            //利用多态调用程序接口
            this->m_EmpArray[i]->showInfo();
        }
    }
}

//按照职工编号判断职工是否存在，若存在，返回职工在数组中的位置，若不存在，则返回-1
int WorkerManager::IsExist(int id)
{
    int index = -1;
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        if(this->m_EmpArray[i]->m_id == id)
        {
            //找到职工
            index = i;
            break;
        }
    }
    return index;
}

//删除职工
void WorkerManager::Del_Emp()
{
    cout << "请输入想删除的职工编号：" << endl;
    int id = 0;
    cin >> id;
    int index = this->IsExist(id);
    if (index != -1)//说明职工存在，并且要删除掉index位置上的职工
    {
        for(int i = index; i < this->m_EmpNum - 1; i++)
        {
            //数据前移
            this->m_EmpArray[i] = this->m_EmpArray[i+1];
        }
        this->m_EmpNum--;//更新数组中记录人员个数
        //数据同步更新到文件中
        this->save();
        cout << "删除成功！" << endl;
    }
    
    else
    {
        cout << "未找到该员工，删除失败！" << endl;
    }
}

//修改职工
void WorkerManager::Mod_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空！" << endl;
    }
    else
    {
        cout << "请输入修改职工的编号：" << endl;
        int id;
        cin >> id;
        
        int ret = this->IsExist(id);
        if(ret != -1)
        {
            //查找到编号的职工
            
            delete this->m_EmpArray[ret];
            int newId = 0;
            string newName;
            int dSelect = 0;
            cout << "查到：" << id << "号职工，请输入新职工号：" << endl;
            cin >> newId;
            cout << "请输入新姓名：" << endl;
            cin >> newName;
            cout << "请输入岗位：" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            cin >> dSelect;
            Worker * worker = NULL;
            switch(dSelect)
            {
                case 1:
                    worker = new Employee(newId, newName, dSelect);
                    break;
                case 2:
                    worker = new Manager(newId, newName, dSelect);
                    break;
                case 3:
                    worker = new Boss(newId, newName, dSelect);
                    break;
                default:
                    break;
            }
            //更新数据到数组中
            this->m_EmpArray[ret] = worker;
            cout << "修改成功！" << endl;
            //保存到文件中
            this->save();
        }
        else
        {
            cout << "查无此人，修改失败！" << endl;
        }
    }
}

//查找职工
void WorkerManager::Find_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空！" << endl;
    }
    else
    {
        cout << "请输入查找的方式：" << endl;
        cout << "1、按职工编号查找" << endl;
        cout << "2、按职工姓名查找" << endl;
        int select = 0;
        cin >> select;
        if (select == 1)
        {
            //按照编号查
            int id;
            cout << "请输入查找的职工编号：" << endl;
            cin >> id;
            int ret = IsExist(id);
            if(ret != -1)
            {
                //找到职工
                cout << "查找成功！该职工信息如下： " << endl;
                this->m_EmpArray[ret]->showInfo();
            }
            else
            {
                cout << "查找失败，查无此人！" << endl;
                
            }
        }
       
        else if (select == 2)
        {
            //按照姓名查找
            string name;
            cout << "请输入您想查找的姓名：" << endl;
            cin >> name;
            //加入判断是否查到的标志
            bool flag = false;
            
            for(int i = 0; i < m_EmpNum; i++)
            {
                if(this->m_EmpArray[i]->m_name == name)
                {
                    cout << "查找成功，该职工信息如下：" << endl;
                    //调用显示信息接口
                    this->m_EmpArray[i]->showInfo();
                    flag = true;
                    
                }
            }
            if (flag == false)
            {
                cout << "查找失败，查无此人！" << endl;
            }
        }
        else
        {
            cout << "输入选项有误！" << endl;
        }
    }
}

//职工排序
void WorkerManager::sort_Emp()
{
    if(this->m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空！" << endl;
    }
    else
    {
        cout << "请输入排序方式：" << endl;
        cout << "1、按职工编号升序排列" << endl;
        cout << "2、按职工编号降序排列" << endl;
        int select = 0;
        cin >> select;
        for(int i = 0; i < m_EmpNum; i++)
        {
            int minOrMax = i;//声明最小值或最大值的下标
            for (int j = i + 1; j < m_EmpNum; j++)
            {
                //升序
                if(select == 1)
                {
                    if( (m_EmpArray[minOrMax]->m_id) > (m_EmpArray[j]->m_id) )
                    {
                        minOrMax = j;
                    }
                }
                //降序
                else
                {
                    if( (m_EmpArray[minOrMax]->m_id) <  (m_EmpArray[j]->m_id) )
                    {
                        minOrMax = j;
                    }
                }
                
            }
            if (i != minOrMax)
            {
                Worker * temp = m_EmpArray[i];
                m_EmpArray[i] = m_EmpArray[minOrMax];
                m_EmpArray[minOrMax] =temp;
            }
        }
        cout << "排序成功，排序后结果为：" << endl;
        this->save();
        this->Show_Emp();
        
    }
}

//清空文件
void WorkerManager::Clean_File()
{
    cout << "确认清空？" << endl;
    cout << "1.确认" << endl;
    cout << "2.返回" << endl;
    int select = 0;
    cin >> select;
    if(select == 1)
    {
        //打开模式 ios::trunc 如果存在，则删除文件并重新创建
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();
        if (this->m_EmpArray != NULL)
        {
            //删除堆区的每一个职工对象
            for(int i = 0; i < this->m_EmpNum; i++)
            {
                if(this->m_EmpArray[i] != NULL)
                {
                    delete this->m_EmpArray[i];
                }
            }
            //删除堆区数组指针
            delete []this->m_EmpArray;
            this->m_EmpNum = 0;
            this->m_EmpArray = NULL;
            this->m_FileIsEmpty = true;
        }
        cout << "清除成功！" << endl;
    }
}

//退出函数
void WorkerManager::ExitSystem()
{
    cout << "欢迎下次使用！\n";
    exit(0);
}

//菜单函数
void WorkerManager::Show_Menu()
{
    cout << "\t欢迎来到职工管理系统" << endl;
    cout << "\t0.退出管理系统\t" << endl;
    cout << "\t1.增加职工信息\t" << endl;
    cout << "\t2.显示职工信息\t" << endl;
    cout << "\t3.删除职工信息\t" << endl;
    cout << "\t4.修改职工信息\t" << endl;
    cout << "\t5.查找职工信息\t" << endl;
    cout << "\t6.按照编号排序\t" << endl;
    cout << "\t7.清空所有文档\t" << endl;
    cout << endl;
    
}
